package com.ruoyi.project.cropmanage.service;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.cropmanage.entity.CropMap;
import com.ruoyi.project.cropmanage.mapper.CropmapMapper;
import com.ruoyi.project.system.dict.domain.DictData;
import com.ruoyi.project.system.dict.service.IDictDataService;
import org.apache.velocity.runtime.directive.Foreach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * huzhonghua
 * 2020/2/10
 * 12:38
 */
@Service
public class CropmapServiceImpl implements ICropmapService {
    @Autowired
    private IDictDataService dictDataService;
    @Autowired
    private CropmapMapper cropmapMapper;
    @Override
    public List<CropMap> selectCropmapList(CropMap cropMap) {
        return cropmapMapper.selectCropmapList(cropMap);
    }

    @Override
    public List<CropMap> selectCropmapAll() {
        return cropmapMapper.selectCropmapAll();
    }

    @Override
    public CropMap selectCropmapById(Long cropMapid) {
        return cropmapMapper.selectCropmapById(cropMapid);
    }

    @Override
    public int deleteCropmapById(Long[] ids) {
        return cropmapMapper.deleteCropmapById(ids);
    }

    @Override
    public int updateCropmap(CropMap cropMap) {
        return cropmapMapper.updateCropmap(cropMap);
    }

    @Override
    public int insertCropmap(CropMap cropMap) {
        cropMap.setStatus("0");
        cropMap.setCreateBy(ShiroUtils.getLoginName());
        return cropmapMapper.insertCropmap(cropMap);
    }

    @Override
    public Map<String, Object> crop_map_getAll(Map<String, Object> map) {
        Map<String, Object> returnMap = new HashMap<>();
        List<Map<String, Object>> resultList =cropmapMapper.crop_map_getAll(map);
        if(resultList.size()==0) {
            returnMap.put("result","empty");
            return returnMap;
        }
        returnMap.put("count", resultList.size());
        returnMap.put("result", resultList);
        return returnMap;
    }

    /**
     * crop_map_getAllok 分类已转成中文
     * @param map
     * @return
     */
    @Override
    public Map<String, Object> crop_map_getAllok(Map<String, Object> map) {
        Map<String, Object> returnMap = new HashMap<>();
        List<Map<String, Object>> resultList =cropmapMapper.crop_map_getAll(map);
        if(resultList.size()==0) {
            returnMap.put("result","empty");
            return returnMap;
        }

        List<DictData> tlist=dictDataService.selectDictDataByType("crop_type");
        for (Map<String,Object> tmpmap :resultList){
            String sort1= tmpmap.get("sort1").toString();
            //按字典value返回中文label
            for (DictData dictData:tlist){
                if(StringUtils.isNotNull(dictData) && dictData.getDictValue().equals(sort1) ){
                    tmpmap.remove("sort1");
                    tmpmap.put("sort1",dictData.getDictLabel());
                }
            }
        }
        returnMap.put("count", resultList.size());
        returnMap.put("result", resultList);
        return returnMap;
    }
}
